 package nccloud.web.pu.storereq.action;
 
 import java.util.List;

import nc.bs.dao.BaseDAO;
import nc.jdbc.framework.processor.ColumnListProcessor;
import nc.vo.pu.m422x.entity.StoreReqAppItemVO;
import nc.vo.pu.m422x.entity.StoreReqAppVO;
 import nc.vo.scmpub.res.billtype.POBillType;
 import nc.vo.scmpub.util.AppInfoContext;
 import nccloud.dto.scmpub.script.entity.SCMScriptResultDTO;
 import nccloud.framework.core.json.IJson;
 import nccloud.framework.core.util.GridCompareUtils;
 import nccloud.framework.service.ServiceLocator;
 import nccloud.framework.web.action.itf.ICommonAction;
 import nccloud.framework.web.container.IRequest;
 import nccloud.framework.web.json.JsonFactory;
 import nccloud.framework.web.ui.pattern.billcard.BillCard;
 import nccloud.framework.web.ui.pattern.billcard.BillCardOperator;
import nccloud.pub.report.resultprocess.ColumnProcessor;
import nccloud.pubitf.riart.pflow.CloudPFlowContext;
 import nccloud.pubitf.scmpub.commit.service.IBatchRunScriptService;
 import nccloud.web.pu.storereq.entity.QueryInfoStorereq;
 import nccloud.web.pu.storereq.util.StoreReqScaleUtil;
 import nccloud.web.scmpub.pub.resexp.PfResumeExceptionNccUtils;

 
 public class StorereqInfoNewSaveAction implements ICommonAction {
    public Object doAction(IRequest request) {
       BillCard card = null;
       BillCard oldCard = null;
       BillCard returnCard = null;
       String read = request.read();
       IJson json = JsonFactory.create();
 
       QueryInfoStorereq query = (QueryInfoStorereq)json.fromJson(read, QueryInfoStorereq.class);
       String type = query.getType();
 
       PfResumeExceptionNccUtils.before(request);
       BillCardOperator operator = new BillCardOperator();
       StoreReqAppVO[] vos = null;
       try {
          StoreReqAppVO vo = (StoreReqAppVO)operator.toBill(request);
          oldCard = operator.toCard(vo);
          this.afterProcess(oldCard);
 
          vo.getParent().setStatus(2);
          StoreReqAppItemVO[]   strAI=(StoreReqAppItemVO[]) vo.getChildrenVO();
          for (StoreReqAppItemVO storeReqAppItemVO : strAI) {
			String material=storeReqAppItemVO.getPk_material();
			String sql="SELECT ipb.norigtaxprice FROM ic_purchasein_b  ipb LEFT JOIN ic_purchasein_h iph" + 
					" ON iph.cgeneralhid = ipb.cgeneralbid WHERE  ipb.cmaterialvid= '"+material+"'    ORDER  BY  iph.creationtime ";
			
	        List<String> messages=(List<String>) new BaseDAO().executeQuery(sql, new ColumnListProcessor());  		
			if(messages.isEmpty()) {
				storeReqAppItemVO.setVbdef2("人工维护");
			}else {
				storeReqAppItemVO.setVbdef2(messages.get(0));
			}
			
		  }
          
          vo.setChildrenVO(strAI);
          vos = new StoreReqAppVO[]{vo};
 
          CloudPFlowContext context = new CloudPFlowContext();
          context.setBillType(POBillType.MRBill.getCode());
          context.setBillVos(vos);
          context.setActionName("SAVEBASE");
 
          AppInfoContext.setBtnCode("Save");
 
          SCMScriptResultDTO obj = ((IBatchRunScriptService)ServiceLocator.find(IBatchRunScriptService.class)).runBacth(context, StoreReqAppVO.class);
 
 
 
          card = operator.toCard(obj.getSucessVOs()[0]);
          this.afterProcess(card);
 
          if (type != null) {
             return card;
          }
          returnCard = GridCompareUtils.compareBillCardGrid(operator.getOriginBillcard(), card);
       } catch (Exception var14) {
          PfResumeExceptionNccUtils.handleResumeException(var14);
       }
       return returnCard;
    }
 
 
 
 
    public void afterProcess(BillCard card) {
       StoreReqScaleUtil stoUtil = new StoreReqScaleUtil();
       stoUtil.processBillCard(card);
    }
 }
